package yxy.game.pm2.command.passbarrier;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.FormatMessage;
import yxy.game.pm2.message.PassBarrierMessage;
import yxy.game.pm2.module.passbarrier.PassbarrierHandle;
import yxy.game.pm2.module.passbarrier.PassbarrierPlayerModule;

import java.util.List;


/****************************************
 * @name CMD_PassBarrierForamtSave
 * @description 斩关夺隘主布阵保存
 * @author 梁健彬
 * @since 2022-09-05
 ***************************************/
@MessageType(proto = Command.PassBarrierForamtSave, classtype = PassBarrierMessage.PassBarrierForamtSaveRequest.class)
public class CMD_PassBarrierForamtSave extends Command<PassBarrierMessage.PassBarrierForamtSaveRequest> {

    @Override
    public void execute(MessageSource messageSource, PassBarrierMessage.PassBarrierForamtSaveRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        List<FormatMessage.Format> formatList = request.getFormatListList();
        PassBarrierMessage.PassBarrierForamtSaveResponse.Builder response = PassBarrierMessage.PassBarrierForamtSaveResponse.newBuilder();
        try {
            PassbarrierPlayerModule module = PassbarrierPlayerModule.get(playerCode);
            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            PassbarrierHandle handle = new PassbarrierHandle();

            //todo 检查队伍并保存
            Result result = handle.formatSet(playerCode, formatList,module);

            send(messageSource, response, result);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, PassBarrierMessage.PassBarrierForamtSaveResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


}
